package Model;

import java.util.LinkedList;
import java.util.Observable;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MoveAction;

import Model.Node;
import View.GamePanel;

public class Snake extends Observable{
	private LinkedList<Node> body;
	
	public Snake() {
		body=new LinkedList<>();
	}

	public void init() {
		body.clear();
		body.addLast(new Node(80, 20));
		body.addLast(new Node(60, 20));
		body.addLast(new Node(40, 20));
		setChanged();
		notifyObservers(body);
	}
	
    private void move() {
    	if (body.size()<3) return;
    	Node tail=body.removeLast();
    	Node head=body.getFirst();
    	int x=head.getX(),y=head.getY();
    	x=x+Node.WIDTH;
    	
    	tail.setX(x);
    	tail.setY(y);body.addFirst(tail);
    	setChanged();
    	notifyObservers(body);
    	
			
		
		
	}
	public void openMoveThread(){
		new Thread( new Runnable() {
			public void run() {
				while (true) {
					move();
					try {
						Thread.sleep(800);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
				
			}
		}).start();
	}

	
	}

